/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view.statistik;

import databasehandling.HandlerFactory;
import java.awt.Color;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import utility.DateUtility;
import utility.statistik.StatistikModul;
import view.MainGUI;

/**
 *
 * @author Jeppe
 */
public class StatistikPanel extends javax.swing.JPanel {

    private boolean settingCombos;

    /**
     * Creates new form StatistikPanel
     *
     * @param mainGUI
     */
    public StatistikPanel(MainGUI mainGUI) {
        initComponents();
        settingCombos = false;
    }

    private void setCombos() {
        settingCombos = true;
        //<editor-fold defaultstate="collapsed" desc="SetCombos">
        fraMånedCombo.removeAllItems();
        fraÅrCombo.removeAllItems();
        tilMånedCombo.removeAllItems();
        tilÅrCombo.removeAllItems();
        for (int i = 0; i < DateUtility.hentMåneder().length; i++) {
            fraMånedCombo.addItem(DateUtility.hentMåneder()[i]);
        }
        for (int i = 0; i < DateUtility.hentÅr().length; i++) {
            fraÅrCombo.addItem(DateUtility.hentÅr()[i]);
        }
        for (int i = 0; i < DateUtility.hentMåneder().length; i++) {
            tilMånedCombo.addItem(DateUtility.hentMåneder()[i]);
        }
        for (int i = 0; i < DateUtility.hentÅr().length; i++) {
            tilÅrCombo.addItem(DateUtility.hentÅr()[i]);
        }
//</editor-fold>
        fraMånedCombo.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH) - 1);
        tilMånedCombo.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));
        settingCombos = false;
    }

    private void setLabels() {
        ugeKnap.setText("Uge " + (Calendar.getInstance().get(Calendar.WEEK_OF_YEAR) - 1));
        månedKnap.setText("Måned: " + DateUtility.hentMåneder()[Calendar.getInstance().getTime().getMonth()]);
        setCombos();

        visStatistik("Idag");
    }

    /**
     * Vis statistik generere en eller flere diagrammer alt efter hvad brugeren
     * vil have genereret statistik over.
     *
     * @param accelerator Knappen-som-kalder-metoden's navn. Enten idag, sidste
     * uge, sidste måned. Eller brugerdef.
     */
    private void visStatistik(String accelerator) {
        String titelPåDiagram = "Antal besøgende ";
        statistikPanel.removeAll();
        int antalBørn = 0;
        int antalVoksne = 0;
        int antalGratister = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        Calendar cal = Calendar.getInstance();
        String[] namesOfDays = new String[]{"Søndag", "Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag", "Lørdag"};
        try {
            if (accelerator.equals("Idag")) {
                //<editor-fold defaultstate="collapsed" desc="SetColors">
                idagKnap.setBackground(MainGUI.BUTTON_SELECTED_COLOR);
                ugeKnap.setBackground(MainGUI.BUTTON_UNSELECTED_COLOR);
                månedKnap.setBackground(MainGUI.BUTTON_UNSELECTED_COLOR);
                brugerDefKnap.setBackground(MainGUI.BUTTON_UNSELECTED_COLOR);
//</editor-fold>
                //<editor-fold defaultstate="collapsed" desc="Beregning af antal Børn, Voksne og gratister">
                antalVoksne = HandlerFactory.getInstance().getStatistikHandler().getAntalVoksneVedDato(cal);
                antalGratister = HandlerFactory.getInstance().getStatistikHandler().getAntalGratisterVedDato(cal);
                antalBørn = HandlerFactory.getInstance().getStatistikHandler().getAntalBørnVedDato(cal);
//</editor-fold>

            } else if (accelerator.equals("Uge")) {
                accelerator = ugeKnap.getText();
                //<editor-fold defaultstate="collapsed" desc="SetColors">
                idagKnap.setBackground(MainGUI.BUTTON_UNSELECTED_COLOR);
                ugeKnap.setBackground(MainGUI.BUTTON_SELECTED_COLOR);
                månedKnap.setBackground(MainGUI.BUTTON_UNSELECTED_COLOR);
                brugerDefKnap.setBackground(MainGUI.BUTTON_UNSELECTED_COLOR);
//</editor-fold>
                //<editor-fold defaultstate="collapsed" desc="Beregning af antal Børn, Voksne og gratister">
                cal.add(Calendar.WEEK_OF_YEAR, -2);

                for (int i = 0; i < 7; i++) {
                    cal.add(Calendar.DAY_OF_MONTH, 1);
//                    System.out.println(namesOfDays[cal.get(Calendar.DAY_OF_WEEK) - 1] + sdf.format(cal.getTime()));
                    antalVoksne = antalVoksne + HandlerFactory.getInstance().getStatistikHandler().getAntalVoksneVedDato(cal);
                    antalGratister = antalGratister + HandlerFactory.getInstance().getStatistikHandler().getAntalGratisterVedDato(cal);
                    antalBørn = antalBørn + HandlerFactory.getInstance().getStatistikHandler().getAntalBørnVedDato(cal);
                }
//</editor-fold>
            } else if (accelerator.equals("Måned")) {
                //<editor-fold defaultstate="collapsed" desc="SetColors">
                idagKnap.setBackground(MainGUI.BUTTON_UNSELECTED_COLOR);
                ugeKnap.setBackground(MainGUI.BUTTON_UNSELECTED_COLOR);
                månedKnap.setBackground(MainGUI.BUTTON_SELECTED_COLOR);
                brugerDefKnap.setBackground(MainGUI.BUTTON_UNSELECTED_COLOR);
//</editor-fold>
                //<editor-fold defaultstate="collapsed" desc="Beregning af antal børn, voksne, gratister for måned">
                accelerator = "i " + DateUtility.hentMåneder()[Calendar.getInstance().get(Calendar.MONTH)];
                cal.set(Calendar.DAY_OF_MONTH, 1);
                int antalDage = Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);
                for (int i = 0; i < antalDage + 1; i++) {
                    if (cal.get(Calendar.DAY_OF_MONTH) <= Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
                        cal.add(Calendar.DAY_OF_MONTH, 1);
//                        System.out.println("Der bliver lagt én til datoen. D." + cal.get(Calendar.DAY_OF_MONTH) + ".");
                        antalVoksne = antalVoksne + HandlerFactory.getInstance().getStatistikHandler().getAntalVoksneVedDato(cal);
                        antalGratister = antalGratister + HandlerFactory.getInstance().getStatistikHandler().getAntalGratisterVedDato(cal);
                        antalBørn = antalBørn + HandlerFactory.getInstance().getStatistikHandler().getAntalBørnVedDato(cal);
//                        System.out.println(cal.get(Calendar.DAY_OF_MONTH) + "," + antalVoksne + "," + antalGratister + "," + antalBørn + " BLAH");
                    }
                }
//                System.out.println(sdf.format(cal.getTime()));
//</editor-fold>
            } else if (accelerator.equals("BrugerDef")) {
                accelerator = "";
                JComboBox standardCombo = new JComboBox();
                Border fejlBorder = new LineBorder(Color.RED, 2);
                //<editor-fold defaultstate="collapsed" desc="SetColors">
                idagKnap.setBackground(MainGUI.BUTTON_UNSELECTED_COLOR);
                ugeKnap.setBackground(MainGUI.BUTTON_UNSELECTED_COLOR);
                månedKnap.setBackground(MainGUI.BUTTON_UNSELECTED_COLOR);
                brugerDefKnap.setBackground(MainGUI.BUTTON_SELECTED_COLOR);
                //</editor-fold>
                boolean succes = true;
                fraMånedCombo.setBorder(standardCombo.getBorder());
                fraÅrCombo.setBorder(standardCombo.getBorder());
                tilMånedCombo.setBorder(standardCombo.getBorder());
                tilÅrCombo.setBorder(standardCombo.getBorder());
                //<editor-fold defaultstate="collapsed" desc="Beregning af antal besøgende">
                if (Calendar.getInstance().get(Calendar.YEAR) == (int) fraÅrCombo.getSelectedItem()) {
                    if (Calendar.getInstance().get(Calendar.MONTH) < fraMånedCombo.getSelectedIndex()) {
                        succes = false;
                        fraMånedCombo.setBorder(fejlBorder);
                    }
                }
                if (Calendar.getInstance().get(Calendar.YEAR) == (int) tilÅrCombo.getSelectedItem()) {
                    if (Calendar.getInstance().get(Calendar.MONTH) < tilMånedCombo.getSelectedIndex()) {
                        succes = false;
                        tilMånedCombo.setBorder(fejlBorder);
                    }
                }
                if (Calendar.getInstance().get(Calendar.YEAR) < (int) fraÅrCombo.getSelectedItem()) {
                    succes = false;
                    fraÅrCombo.setBorder(fejlBorder);
                }
                if (Calendar.getInstance().get(Calendar.YEAR) < (int) tilÅrCombo.getSelectedItem()) {
                    succes = false;
                    tilÅrCombo.setBorder(fejlBorder);
                }
                if ((int) fraÅrCombo.getSelectedItem() > (int) tilÅrCombo.getSelectedItem()) {
                    succes = false;
                    tilÅrCombo.setBorder(fejlBorder);
                }
                if ((int) fraÅrCombo.getSelectedItem() == (int) tilÅrCombo.getSelectedItem() && succes) {
                    if (fraMånedCombo.getSelectedIndex() > tilMånedCombo.getSelectedIndex()) {
                        succes = false;
                        fraMånedCombo.setBorder(fejlBorder);
                    }
                }
                if(succes){
                    Calendar fraDato = Calendar.getInstance();
                    fraDato.setTime(sdf.parse("01." + (fraMånedCombo.getSelectedIndex() + 1) + "." + (int) fraÅrCombo.getSelectedItem()));
                    Calendar tilDato = Calendar.getInstance();
                    tilDato.setTime(sdf.parse(fraDato.getActualMaximum(Calendar.DAY_OF_MONTH) + "." + (tilMånedCombo.getSelectedIndex() + 1) + "." + (int) tilÅrCombo.getSelectedItem()));
                    if (fraDato.before(tilDato)) {
                        if (fraMånedCombo.getSelectedIndex() == tilMånedCombo.getSelectedIndex() && fraÅrCombo.getSelectedIndex() == tilÅrCombo.getSelectedIndex()) {
                            accelerator = "i " + DateUtility.hentMåneder()[fraMånedCombo.getSelectedIndex()];
                        } else if (fraÅrCombo.getSelectedIndex() == tilÅrCombo.getSelectedIndex()) {
                            accelerator = "fra " + DateUtility.hentMåneder()[fraMånedCombo.getSelectedIndex()] + " til " + DateUtility.hentMåneder()[tilMånedCombo.getSelectedIndex()];
                        } else {
                            accelerator = "fra " + DateUtility.hentMåneder()[fraMånedCombo.getSelectedIndex()] + ", " + DateUtility.hentÅr()[fraÅrCombo.getSelectedIndex()] + " til " + DateUtility.hentMåneder()[tilMånedCombo.getSelectedIndex()] + ", " + DateUtility.hentÅr()[tilÅrCombo.getSelectedIndex()];
                        }
                        int antalDage = DateUtility.daysBetween(fraDato.getTime(), tilDato.getTime());//                                    
                        for (int i = 0; i < antalDage + 1; i++) {
                            fraDato.add(Calendar.DAY_OF_MONTH, 1);
                            antalVoksne = antalVoksne + HandlerFactory.getInstance().getStatistikHandler().getAntalVoksneVedDato(fraDato);
                            antalGratister = antalGratister + HandlerFactory.getInstance().getStatistikHandler().getAntalGratisterVedDato(fraDato);
                            antalBørn = antalBørn + HandlerFactory.getInstance().getStatistikHandler().getAntalBørnVedDato(fraDato);
                        }
                    } else {
                        titelPåDiagram = "Ingen Besøgende i den valgte periode";
                    }
                }else{
                     titelPåDiagram = "Den valgte periode er ugyldig";
                }

//</editor-fold>
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Fejl 1009 \nFejl ved indlæsning af varer fra databasen. Prøv evt. igen senere. \n" + ex.getLocalizedMessage(), "Database Timeout", 0);
        }catch (ParseException ex) {
            JOptionPane.showMessageDialog(this, "Fejl 1025 \nFejl ved formatering af dato", "Formateringsfejl", 0);
        }
        StatistikModul statistikModul = new StatistikModul(titelPåDiagram + accelerator);
        statistikModul.addStatistikData("Antal Børn", antalBørn, new Color(165, 14, 130));
        statistikModul.addStatistikData("Antal Voksne", antalVoksne, new Color(20, 150, 124));
        statistikModul.addStatistikData("Antal Gratister", antalGratister, new Color(5, 47, 97));
        statistikModul.generérStatistik();
        statistikPanel.add(statistikModul);
        statistikModul.setBounds(0, 0, 703, 470);
        statistikModul.setVisible(true);
        statistikPanel.revalidate();
        statistikPanel.repaint();
        statistikModul.revalidate();
        statistikModul.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        idagKnap = new javax.swing.JButton();
        ugeKnap = new javax.swing.JButton();
        månedKnap = new javax.swing.JButton();
        brugerDefknapPanel = new javax.swing.JPanel();
        fraMånedCombo = new javax.swing.JComboBox();
        fraÅrCombo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tilMånedCombo = new javax.swing.JComboBox();
        tilÅrCombo = new javax.swing.JComboBox();
        brugerDefKnap = new javax.swing.JButton();
        statistikPanel = new javax.swing.JPanel();

        setOpaque(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        setLayout(null);

        jPanel2.setOpaque(false);
        jPanel2.setLayout(null);

        idagKnap.setText("Idag");
        idagKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                idagKnapActionPerformed(evt);
            }
        });
        jPanel2.add(idagKnap);
        idagKnap.setBounds(0, 0, 200, 100);

        ugeKnap.setText("1 Uge");
        ugeKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ugeKnapActionPerformed(evt);
            }
        });
        jPanel2.add(ugeKnap);
        ugeKnap.setBounds(200, 0, 200, 100);

        månedKnap.setText("1 Måned");
        månedKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                månedKnapActionPerformed(evt);
            }
        });
        jPanel2.add(månedKnap);
        månedKnap.setBounds(400, 0, 200, 100);

        brugerDefknapPanel.setLayout(null);

        fraMånedCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                fraMånedComboItemStateChanged(evt);
            }
        });
        brugerDefknapPanel.add(fraMånedCombo);
        fraMånedCombo.setBounds(32, 10, 90, 30);

        fraÅrCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                fraÅrComboItemStateChanged(evt);
            }
        });
        brugerDefknapPanel.add(fraÅrCombo);
        fraÅrCombo.setBounds(120, 10, 70, 30);

        jLabel1.setText("Fra:");
        brugerDefknapPanel.add(jLabel1);
        jLabel1.setBounds(10, 20, 30, 14);

        jLabel2.setText("Til:");
        brugerDefknapPanel.add(jLabel2);
        jLabel2.setBounds(10, 58, 20, 14);

        tilMånedCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                tilMånedComboItemStateChanged(evt);
            }
        });
        brugerDefknapPanel.add(tilMånedCombo);
        tilMånedCombo.setBounds(32, 50, 90, 30);

        tilÅrCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                tilÅrComboItemStateChanged(evt);
            }
        });
        brugerDefknapPanel.add(tilÅrCombo);
        tilÅrCombo.setBounds(120, 50, 70, 30);

        brugerDefKnap.setFocusable(false);
        brugerDefKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brugerDefKnapActionPerformed(evt);
            }
        });
        brugerDefknapPanel.add(brugerDefKnap);
        brugerDefKnap.setBounds(0, 0, 200, 100);

        jPanel2.add(brugerDefknapPanel);
        brugerDefknapPanel.setBounds(600, 0, 202, 100);

        add(jPanel2);
        jPanel2.setBounds(0, 0, 810, 100);

        statistikPanel.setOpaque(false);
        statistikPanel.setLayout(null);
        add(statistikPanel);
        statistikPanel.setBounds(0, 100, 800, 510);
    }// </editor-fold>//GEN-END:initComponents

    private void idagKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_idagKnapActionPerformed
        visStatistik("Idag");
    }//GEN-LAST:event_idagKnapActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        setLabels();
    }//GEN-LAST:event_formComponentShown

    private void ugeKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ugeKnapActionPerformed
        visStatistik("Uge");
    }//GEN-LAST:event_ugeKnapActionPerformed

    private void månedKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_månedKnapActionPerformed
        visStatistik("Måned");
    }//GEN-LAST:event_månedKnapActionPerformed

    private void fraMånedComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_fraMånedComboItemStateChanged
        if (!settingCombos) {
            visStatistik("BrugerDef");
        }
    }//GEN-LAST:event_fraMånedComboItemStateChanged

    private void fraÅrComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_fraÅrComboItemStateChanged
        if (!settingCombos) {
            visStatistik("BrugerDef");
        }
    }//GEN-LAST:event_fraÅrComboItemStateChanged

    private void tilMånedComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_tilMånedComboItemStateChanged
        if (!settingCombos) {
            visStatistik("BrugerDef");
        }
    }//GEN-LAST:event_tilMånedComboItemStateChanged

    private void tilÅrComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_tilÅrComboItemStateChanged
        if (!settingCombos) {
            visStatistik("BrugerDef");
        }
    }//GEN-LAST:event_tilÅrComboItemStateChanged

    private void brugerDefKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_brugerDefKnapActionPerformed
        if (!settingCombos) {
            visStatistik("BrugerDef");
        }
    }//GEN-LAST:event_brugerDefKnapActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton brugerDefKnap;
    private javax.swing.JPanel brugerDefknapPanel;
    private javax.swing.JComboBox fraMånedCombo;
    private javax.swing.JComboBox fraÅrCombo;
    private javax.swing.JButton idagKnap;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton månedKnap;
    private javax.swing.JPanel statistikPanel;
    private javax.swing.JComboBox tilMånedCombo;
    private javax.swing.JComboBox tilÅrCombo;
    private javax.swing.JButton ugeKnap;
    // End of variables declaration//GEN-END:variables
}
